﻿using UnityEngine;
using System.Collections;
namespace Socket.Core
{
    using Socket.Core.Error;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;

    public class ServerCommand : IPackageCommand
    {
        private List<IReceiveInfo> AddStream(List<IReceiveInfo> list, ISession session)
        {
            if (list == null)
            {
                list = new List<IReceiveInfo>();
            }
            IReceiveInfo receiveData = session.ReceiveData;
            list.Add(receiveData);
            session.InitReceive();
            return list;
        }

        private IReceiveInfo InitializeStream(ISession session)
        {
            if (session.ReceiveData == null)
            {
                ReceiveInfo info2 = new ReceiveInfo {
                    ReceiveTime = DateTime.Now,
                    ReceiveStream = new MemoryStream(),
                    ReceiveLength = -1,
                    ReceiveHead = new byte[this.MessageHeadLength],
                    ReceiveHeadOffset = 0
                };
                IReceiveInfo info = info2;
                session.ReceiveData = info;
                return info;
            }
            return session.ReceiveData;
        }

        public List<IReceiveInfo> PartPackageCommand(ISession session, byte[] bytes, int Offset, int Length)
        {
			//Debug.Log(string.Format("========PartPackageCommand"));
            if (session == null)
            {
                return null;
            }
			//Debug.Log(string.Format("========PartPackageCommand 3333"));
            List<IReceiveInfo> list = null;
            int num = 0;
            int count = 0;
            do
            {
                IReceiveInfo info = this.InitializeStream(session);
                if ((this.MessageHeadLength - info.ReceiveHeadOffset) > 0)
                {
                    if ((Length - num) >= (this.MessageHeadLength - info.ReceiveHeadOffset))
                    {
                        count = this.MessageHeadLength - info.ReceiveHeadOffset;
                        Buffer.BlockCopy(bytes, Offset + num, info.ReceiveHead, info.ReceiveHeadOffset, count);
                        num += count;
                        info.ReceiveHeadOffset += count;
                    }
                    else
                    {
                        count = Length - num;
                        Buffer.BlockCopy(bytes, Offset + num, info.ReceiveHead, info.ReceiveHeadOffset, Length - num);
                        info.ReceiveHeadOffset += count;
                        num += count;
                    }
                    if (((this.MessageHeadLength - info.ReceiveHeadOffset) == 0) && (info.ReceiveLength < 0))
                    {
                        int len = System.BitConverter.ToInt32(info.ReceiveHead, 0);
                        int id = System.BitConverter.ToInt32(info.ReceiveHead, 4);

                        info.ReceiveLength = System.Net.IPAddress.NetworkToHostOrder(len);
                        info.MethodCode = System.Net.IPAddress.NetworkToHostOrder(id);

                        if (info.ReceiveLength > this.Config.MaxMessageLength)
                        {
                            throw new ServerException(ServerExceptionType.MessageLength, "消息体包长度不正确");
                        }
                        if (info.ReceiveLength == 0)
                        {
                            info.IsHeartbeat = true;
                            list = this.AddStream(list, session);
                        }
                    }
                }
                else if ((info.ReceiveLength - info.ReceiveStream.Length - this.MessageHeadLength) <= (Length - num))
                {
                    count = info.ReceiveLength - (int)info.ReceiveStream.Length - this.MessageHeadLength;
                    info.ReceiveStream.Write(bytes, Offset + num, count);
                    num += count;
                    list = this.AddStream(list, session);
                }
                else if ((info.ReceiveLength - info.ReceiveStream.Length - this.MessageHeadLength) > (Length - num))
                {
                    info.ReceiveStream.Write(bytes, Offset + num, Length - num);
                    num += Length - num;
                }
                if (num > Length)
                {
                    throw new ServerException(ServerExceptionType.MessageLength, "消息体包长度不正确");
                }
            }
            while (num != Length);

			//Debug.Log(string.Format("========PartPackageCommand end"));
            return list;
        }

        public ConfigAbstract Config { get; set; }

        public int MessageHeadLength
        {
            get
            {
                return 16;
            }
        }
    }
}

